home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
AOL File Library: 2,801 to 2,900
/
aol-file-protocol-4400-2801-to-2900.zip
/
AOLDLs
/
C++ Files Library
/
C++ Direct Buffer Access Code
/
Blitting.sit
/
Blitting Folder ƒ
/
MySprite ƒ
/
MySpriteMain.cp
< prev
next >
Wrap
Text File
|
1995-08-04
|
5KB
|
194 lines
// MySpriteMain.cp, the main source file for MySprite. Used to test the Sprite
// classes. This runs in four main steps: (1) every object on the screen is
// moved; (2) every old posistion is erased; (3) the objects are drawn in their
// new posistions; and (4) the buffer is copied to the window.
//
// The purpose of this project is to show how the blitters can be used... while
// doing it, I found out (in profilings) that Duff's device isn't so hot, but
// bypassing CopyBits still is.
// copyright ⌐ 1995, Macneil Shonle. All rights reserved.
#include <InitToolbox.h>
#include <SecureWindowRef.h>
#include <SecureGWorld.h>
#include <WindowAccessor.h>
#include <GWorldAccessor.h>
#include <Sprite.h>
#include "ForegroundObject.h"
#include <MacintoshIdioms.h>
#include <GWorldSetter.h>
#include <MMUModeSwapper.h>
#include <vector.h>
#include <profiler.h>
void runProgram();
main()
{
#if __profile__
if (!ProfilerInit(collectDetailed, bestTimeBase, 80, 20)) {
#endif
try {
runProgram();
}
catch (xalloc& x) {
::DebugStr68k("\pxalloc caught, try adding more memory");
}
catch (...) {
::DebugStr68k("\punknown error occured");
}
#if __profile__
ProfilerDump( "\pUnrolled.prof" );
ProfilerTerm();
}
#endif
return 0;
}
void ProfilerCopyBits(SecureMacPort& source, SecureMacPort& dest,
const Rect& sourceR, const Rect& destR)
{
GWorldSetter setTo(dest.getMacPort(), dest.getMacGD());
::CopyBits(source.getBitMap(), dest.getBitMap(),
&sourceR, &destR, srcCopy, nil);
}
void CopyImage(BufferAccessor& src, BufferAccessor& dest,
const Rect& srcR, const Rect& destR)
{
FourPixelsPtr srcPtr = (FourPixelsPtr)src.getAddrOfPixel(srcR.left, srcR.top);
FourPixelsPtr destPtr = (FourPixelsPtr)dest.getAddrOfPixel(destR.left, destR.top);
PixelCord copyWidth = srcR.right - srcR.left;
PixelCord copyWidthDiv4 = copyWidth / 4;
PixelCord copyHeight = srcR.bottom - srcR.top;
RowBytes srcRowSkip = src.getRowBytes() - copyWidth;
RowBytes destRowSkip = dest.getRowBytes() - copyWidth;
MMUModeSwapper swapTo(src.use32Bit() | dest.use32Bit());
for (PixelCord y=0; y<copyHeight; y++) {
for (PixelCord x=0; x<copyWidthDiv4; x++)
*destPtr++ = *srcPtr++;
if (copyWidth & 01) {
*PixelPtr(destPtr) = *PixelPtr(srcPtr);
destPtr = FourPixelsPtr(PixelPtr(destPtr) + 1);
srcPtr = FourPixelsPtr(PixelPtr(srcPtr) + 1);
}
if (copyWidth & 02) {
*TwoPixelsPtr(destPtr) = *TwoPixelsPtr(srcPtr);
destPtr = FourPixelsPtr(PixelPtr(destPtr) + 2);
srcPtr = FourPixelsPtr(PixelPtr(srcPtr) + 2);
}
srcPtr = FourPixelsPtr(PixelPtr(srcPtr) + srcRowSkip);
destPtr = FourPixelsPtr(PixelPtr(destPtr) + destRowSkip);
}
}
// returns random number between [1...high], inclusive
inline short RangeRandom(short high)
{
short number = ::Random() % high;
number = (number < 0) ? -number : number;
return number + 1;
}
void runProgram()
{
InitToolbox();
qd.randSeed = 1; // random, but same for each launch. For "fair" profiling
PixPatHandle ppat = ::GetPixPat(128);
CIconHandle ballCicn = ::GetCIcon(128);
if (ballCicn == 0) throw xalloc("GetCIcon failed", "runProgram");
CIconHandle redHoleCicn = ::GetCIcon(129);
if (redHoleCicn == 0) throw xalloc("GetCIcon failed", "runProgram");
Rect bounds = {0, 0, 342, 512};
CenterRect(bounds, qd.screenBits.bounds);
SecureWindowRef window(bounds, documentProc, "\pMySprite");
WindowAccessor windowBuff = window;
SecureGWorld gworld(window.getPortRect());
GWorldAccessor gworldBuff = gworld;
SecureGWorld virgin(window.getPortRect());
GWorldAccessor virginBuff = virgin;
{
GWorldSetter setTo(virgin);
::FillCRect(&virgin.getPortRect(), ppat);
}
{
GWorldSetter setTo(gworld);
ProfilerCopyBits(virgin, gworld, virgin.getPortRect(), gworld.getPortRect());
}
{
GWorldSetter setTo(window);
ProfilerCopyBits(gworld, window, gworld.getPortRect(), window.getPortRect());
}
CIconSprite ballSprite(ballCicn);
CIconSprite redHoleSprite(redHoleCicn);
const PixelCord kMaxVelocity = 7;
vector<BouncingObject*> ballVect(16, 0);
for (int i=0; i<ballVect.size(); i++) {
Rect randomRect;
randomRect.left = RangeRandom(gworldBuff.getWidth() - ballSprite.getWidth());
randomRect.top = RangeRandom(gworldBuff.getHeight() - ballSprite.getHeight());
randomRect.right = randomRect.left + ballSprite.getWidth();
randomRect.bottom = randomRect.top + ballSprite.getHeight();
ballVect[i] = new BouncingObject(ballSprite, randomRect,
RangeRandom(kMaxVelocity), RangeRandom(kMaxVelocity), kMaxVelocity+2);
}
int numFrames = 6000;
::HideCursor();
{
GWorldSetter setTo(gworld);
while (!::Button() && (--numFrames > 0)) {
for (int i=0; i<ballVect.size(); i++)
ballVect[i]->move(gworldBuff);
for (int i=0; i<ballVect.size(); i++)
// ::ProfilerCopyBits(virgin, gworld,
// ballVect[i]->getOldRect(), ballVect[i]->getOldRect());
CopyImage(virginBuff, gworldBuff, ballVect[i]->getOldRect(),
ballVect[i]->getOldRect());
for (int i=0; i<ballVect.size(); i++)
ballVect[i]->draw(gworldBuff);
for (int i=0; i<ballVect.size(); i++) {
Rect oldAndNew;
ballVect[i]->getOldAndNew(oldAndNew);
// ProfilerCopyBits(gworld, window, oldAndNew, oldAndNew);
CopyImage(gworldBuff, windowBuff, oldAndNew, oldAndNew);
}
}
}
::ShowCursor();
for (int i=0; i<ballVect.size(); i++)
delete ballVect[i];
::DisposeCIcon(ballCicn);
::DisposePixPat(ppat);
::FlushEvents(everyEvent, 0);
}